import os
import datetime
from django.shortcuts import render
from django.http import FileResponse
from BigDataWeb.view import get_algorithm
from BigDataWeb.view import put_algorithm
from BigDataWeb.view import read_data_source
from data_visualization.simple_chart import SimpleChart
from data_visualization.combo_chart import ComboChart
from data_visualization.map_marker import MapMarker
from data_visualization.heat_map import HeatMap
from data_visualization.simple_plt_chart import SimplePltChart
from data_visualization.combo_plt_chart import ComboPltChart


def simple_chart(request):
    context = {}
    algorithm = SimpleChart()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "data_visualization/1.html", context)


def simple_plt_chart(request):
    context = {}
    algorithm = SimplePltChart()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "data_visualization/1.html", context)


def combo_chart(request):
    context = {}
    algorithm = ComboChart()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "data_visualization/1.html", context)


def combo_plt_chart(request):
    context = {}
    algorithm = ComboPltChart()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "data_visualization/1.html", context)


def map_marker(request):
    context = {}
    algorithm = MapMarker()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "data_visualization/1.html", context)


def heat_map(request):
    context = {}
    algorithm = HeatMap()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "data_visualization/1.html", context)


def select_data_source(request):
    context = {}
    algorithm = get_algorithm(request)
    read_data_source(request, algorithm)
    context["algorithm"] = algorithm
    return render(request, "data_visualization/2.html", context)


def understand_data_set(request):
    context = {}
    algorithm = get_algorithm(request)
    
    if algorithm.algorithm_name == "简单图表":
        algorithm.setXAxisAndYAxisName(request.POST.get("xAxis_name"), request.POST.get("yAxis_name"))
        
    if algorithm.algorithm_name == "复合图表":
        algorithm.setXAxisAndYAxisName(request.POST.get("xAxis_name"), request.POST.get("yAxis_name_1"), request.POST.get("yAxis_name_2"))
    
    if algorithm.algorithm_name == "地图标注":
        algorithm.setLngLatAndTitleName(request.POST.get("lng_name"), request.POST.get("lat_name"), request.POST.get("title_name"))
    
    if algorithm.algorithm_name == "热力图":
        algorithm.setLngLatAndValueName(request.POST.get("lng_name"), request.POST.get("lat_name"), request.POST.get("value_name"))
    
    if algorithm.algorithm_name == "简单Matplotlib图表":
        algorithm.setXLabel(request.POST.get("xAxis_name"))
        algorithm.setYLabel(request.POST.get("yAxis_name"))
    
    if algorithm.algorithm_name == "复合Matplotlib图表":
        algorithm.setXLabel(request.POST.get("xAxis_name"))
        algorithm.setYLabels(request.POST.get("yAxis_name_1"), request.POST.get("yAxis_name_2"))
    
    context["algorithm"] = algorithm
    return render(request, "data_visualization/3.html", context)


def configure_parameters(request):
    context = {}
    algorithm = get_algorithm(request)
    
    if algorithm.algorithm_name == "简单图表":
        algorithm.chart_title = request.POST.get("chart_title")
        algorithm.chart_type = request.POST.get("chart_type")
        
    if algorithm.algorithm_name == "复合图表":
        algorithm.chart_title = request.POST.get("chart_title")
        algorithm.chart_type_1 = request.POST.get("chart_type_1")
        algorithm.chart_type_2 = request.POST.get("chart_type_2")
        algorithm.use_2nd_axis = bool(request.POST.get("use_2nd_axis"))
        
    if algorithm.algorithm_name == "地图标注":
        algorithm.overlay_type = request.POST.get("overlay_type")
        
    if algorithm.algorithm_name == "热力图":
        algorithm.radius = float(request.POST.get("radius"))
        algorithm.opacity = float(request.POST.get("opacity"))
        algorithm.max = float(request.POST.get("max"))
        
    if algorithm.algorithm_name == "简单Matplotlib图表":
        algorithm.chart_title = request.POST.get("chart_title")
        algorithm.chart_type = request.POST.get("chart_type")
        
    if algorithm.algorithm_name == "复合Matplotlib图表":
        algorithm.chart_title = request.POST.get("chart_title")
        algorithm.chart_type_1 = request.POST.get("chart_type_1")
        algorithm.chart_type_2 = request.POST.get("chart_type_2")
        algorithm.use_2nd_axis = bool(request.POST.get("use_2nd_axis"))
    
    algorithm.implent()
    
    if algorithm.algorithm_name == "简单Matplotlib图表" or algorithm.algorithm_name == "复合Matplotlib图表":
        algorithm.generateNotebookFile()
    else:
        algorithm.generateHtmlFile()
        
    context["algorithm"] = algorithm
    if algorithm.algorithm_name == "简单Matplotlib图表" or algorithm.algorithm_name == "复合Matplotlib图表":
        return render(request, "data_visualization/5_1.html", context)
    else:
        return render(request, "data_visualization/5.html", context)


def download_html_file(request):
    html_file_name = "page_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") + ".html"
    html_file_path = os.path.join(get_algorithm(request).work_folder_path, html_file_name)
    with open(html_file_path, "w", encoding="utf-8") as f:
        f.write(request.POST.get("code"))
    response = FileResponse(open(html_file_path, "rb"))
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment;filename="%s"' % (html_file_name)
    return response
    
